package com.zbkj.crmeb.merchant.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.common.CommonPage;
import com.crmeb.core.common.CommonResult;
import com.common.PageParamRequest;
import com.github.pagehelper.PageInfo;
import com.utils.CrmebUtil;
import com.zbkj.crmeb.merchant.admin.service.SupplierStoreProductService;
import com.zbkj.crmeb.store.model.StoreProduct;
import com.zbkj.crmeb.store.model.StoreProductUpload;
import com.zbkj.crmeb.store.request.BatchEnabledRequest;
import com.zbkj.crmeb.store.request.SupplierProductRequest;
import com.zbkj.crmeb.store.request.SupplierStoreProductSearchRequest;
import com.zbkj.crmeb.store.response.StoreProductNormResponse;
import com.zbkj.crmeb.store.response.StoreProductResponse;
import com.zbkj.crmeb.store.response.StoreProductTabsHeader;
import com.zbkj.crmeb.store.service.StoreProductMerService;
import com.zbkj.crmeb.store.vo.StoreProductMerVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 供应商-商品管理
 */

@RestController
@RequestMapping("api/admin/supplier/store/product")
@Api(tags = "供应商-商品管理")
public class SupplierStoreProductController extends AdminBaseController {

    @Autowired
    private SupplierStoreProductService supplierStoreProductService;
    @Autowired
    private StoreProductMerService storeProductMerService;

    @ApiOperation(value = "供应商商品分页列表")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<StoreProductResponse>> supplierProductList(SupplierStoreProductSearchRequest request,HttpServletRequest servletRequest) {
        PageInfo<StoreProductResponse> pageInfo = supplierStoreProductService.supplierProductList(request,getMerId(servletRequest));
        if (pageInfo == null) {
            return CommonResult.success(CommonPage.restPage(new ArrayList<>()));
        }
        return CommonResult.success(CommonPage.restPage(pageInfo));
    }

    @ApiOperation(value = "草稿箱商品列表")
    @GetMapping(value = "/draft/list")
    public CommonResult<CommonPage<StoreProductResponse>> supplierDraftProductList(SupplierStoreProductSearchRequest request,HttpServletRequest servletRequest) {
        PageInfo<StoreProductResponse> pageInfo = supplierStoreProductService.supplierDraftProductList(request,getMerId(servletRequest));
        if (pageInfo == null) {
            return CommonResult.success(CommonPage.restPage(new ArrayList<>()));
        }
        return CommonResult.success(CommonPage.restPage(pageInfo));
    }

    @ApiOperation(value = "商品详情")
    @GetMapping(value = "/info/{id}")
    public CommonResult<StoreProductNormResponse> supplierProductInfo(@PathVariable Integer id,HttpServletRequest servletRequest) {
        StoreProductNormResponse storeProductResponse = supplierStoreProductService.supplierProductInfo(id,getMerId(servletRequest));
        return CommonResult.success(storeProductResponse);
    }

    @ApiOperation(value = "草稿箱商品详情")
    @GetMapping(value = "/draft/info/{id}")
    public CommonResult<StoreProductResponse> supplierDraftProductInfo(@PathVariable Integer id,HttpServletRequest servletRequest) {
        StoreProductResponse response = supplierStoreProductService.supplierDraftProductInfo(id,getMerId(servletRequest));
        return CommonResult.success(response);
    }

    @ApiOperation(value = "新增商品")
    @PostMapping(value = "/add")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> addSupplierProduct(@RequestBody @Validated SupplierProductRequest supplierProductRequest,HttpServletRequest servletRequest) {
        // 新增商品
        boolean b = supplierStoreProductService.addSupplierProduct(supplierProductRequest, getMerId(servletRequest));
        if(!b) return CommonResult.failed();

        Integer id = supplierStoreProductService.lambdaQuery()
                // .eq(StoreProduct::getMerId, getMerId(servletRequest))
                // .eq(StoreProduct::getStoreName, supplierProductRequest.getStoreName())
                .eq(StoreProduct::getBarCode, supplierProductRequest.getBarCode())
                .orderByDesc(StoreProduct::getId) // 按 id 字段降序排序
                .last("LIMIT 1") // 限制结果集大小为 1
                .one() // 获取单个结果
                .getId();

        // 默认启用商品, 并在商户商品动态表添加数据
        String token = checkAdminToken.getTokenFormRequest(servletRequest);
        StoreProductMerVo productMerVo = new StoreProductMerVo()
                .setProductId(id)
                .setIsNotShow("0");
        return storeProductMerService.enabled(token, productMerVo) ? CommonResult.success() : CommonResult.failed();
    }

    @ApiOperation(value = "编辑商品")
    @PutMapping(value = "/update")
    public CommonResult<String> updateSupplierProduct(@RequestBody @Validated SupplierProductRequest supplierProductRequest,HttpServletRequest servletRequest) {
        return supplierStoreProductService.updateSupplierProduct(supplierProductRequest,getMerId(servletRequest)) ? CommonResult.success() : CommonResult.failed();
    }

    @ApiOperation(value = "批量编辑商品")
    @PutMapping(value = "/batch/update")
    public CommonResult<String> batchUpdateSupplierProduct(@RequestBody SupplierProductRequest supplierProductRequest,HttpServletRequest servletRequest) {
        return supplierStoreProductService.batchUpdateSupplierProduct(supplierProductRequest,getMerId(servletRequest)) ? CommonResult.success() : CommonResult.failed();
    }

    @ApiOperation(value = "编辑草稿箱商品")
    @PutMapping(value = "/draft/update")
    public CommonResult<String> updateSupplierDraftProduct(@RequestBody @Validated SupplierProductRequest supplierProductRequest,HttpServletRequest servletRequest) {
        return supplierStoreProductService.updateSupplierDraftProduct(supplierProductRequest,getMerId(servletRequest)) ? CommonResult.success() : CommonResult.failed();
    }

    @ApiOperation(value = "删除草稿箱商品")
    @DeleteMapping(value = "/draft/delete")
    public CommonResult<String> deleteSupplierDraftProduct(@ApiParam(name = "ids",value = "商品id",required = true) @RequestParam List<Integer> ids,HttpServletRequest servletRequest) {
        return supplierStoreProductService.deleteSupplierDraftProduct(ids,getMerId(servletRequest)) ? CommonResult.success() : CommonResult.failed();
    }

    @PutMapping("/batch/show")
    @ApiOperation("批量上架/下架")
    public CommonResult<Object> batchShow(@RequestBody @Validated BatchEnabledRequest request, HttpServletRequest servletRequest) {
        return CommonResult.success(supplierStoreProductService.batchShow(request,getMerId(servletRequest)));
    }

    @ApiOperation(value = "上传记录")
    @GetMapping(value = "upload/list")
    public CommonResult<CommonPage<StoreProductUpload>> uploadList(PageParamRequest request, HttpServletRequest servletRequest) {
        PageInfo<StoreProductUpload> pageInfo = supplierStoreProductService.uploadList(request,getMerId(servletRequest));
        return CommonResult.success(CommonPage.restPage(pageInfo));
    }

    @ApiOperation(value = "供应商商品表头数量")
    @RequestMapping(value = "/tabs/headers", method = RequestMethod.GET)
    public CommonResult<List<StoreProductTabsHeader>> getTabsHeader(HttpServletRequest servletRequest) {
        return CommonResult.success(supplierStoreProductService.getTabsHeader(getMerId(servletRequest)));
    }

    @ApiOperation(value = "批量下架商品")
    @RequestMapping(value = "/batch/undercarriage", method = RequestMethod.POST)
    public CommonResult<Boolean> batchUndercarriage(@RequestBody String productIds, HttpServletRequest servletRequest) {
        List<Integer> productIdList = CrmebUtil.stringToArray(JSON.parseObject(productIds).getString("productIds"));
        return CommonResult.success(supplierStoreProductService.batchUndercarriage(productIdList, getMerId(servletRequest)));
    }

}
